Skip to content

Conversation

@nperez0111
Copy link
Contributor

This change introduces a beforeSync hook which is invoked just before the Y.js update is applied to the Y doc.
This is different from the existing onChange hook in that the update has not yet been applied to the document.

We have a use-case where we want to inspect the contents of an update before it is actually applied to the Y doc.
This specifically would enable us to revert changes to the document, if we consider them to be invalid, you can read more information here: TypeCellOS/BlockNote-demo-nextjs-hocuspocus#1

We were able to use the beforeHandleMessage hook for this (implemented here), but it required us to skip over parts of the hocuspocus binary message format, which we felt was brittle.
This beforeSync hook allows us to inspect the Y.js update at the y-sync/protocols level which would be ideal context to revert messages with.

@nperez0111
Copy link
Contributor Author

I based the PR off of the branch in this PR #917 I couldn't figure out how to make a PR into it here, but only the last commit is different between the two

@janthurau janthurau force-pushed the before-sync-callback branch 2 times, most recently from f49a7f9 to 9f32354 Compare March 27, 2025 08:42
@nperez0111 nperez0111 force-pushed the before-sync-callback branch from 9f32354 to 3d350b1 Compare March 27, 2025 11:30
@nperez0111 nperez0111 force-pushed the before-sync-callback branch from 3d350b1 to c7b96cc Compare March 27, 2025 11:35
@janthurau janthurau merged commit a6a7bcd into ueberdosis:main Mar 28, 2025
6 checks passed
@nperez0111
Copy link
Contributor Author

Thanks @janthurau!

janthurau added a commit that referenced this pull request Oct 22, 2025
…6a7bcd). Making applySync async causes timing issues that leads to issues when using PermanentUserData. This can be reproduced by using the playground server with sqlite and a sleep(1000). Additionally, use Y.PermanentUserData, or try to read data from the ydoc in the onSynced event.
janthurau added a commit that referenced this pull request Oct 23, 2025
… reverts a6a7bcd). Making applySync async causes timing issues that leads to issues when using PermanentUserData. This can be reproduced by using the playground server with sqlite and a sleep(1000). Additionally, use Y.PermanentUserData, or try to read data from the ydoc in the onSynced event."

This reverts commit 7192664.
@janthurau
Copy link
Collaborator

janthurau commented Oct 23, 2025

This causes issues because of yjs/yjs#591, will most likely need to revert this, or at least make sure that Sync1+2 happen before any update message is applied by the MessageReceiver ; our PermanentUserData implementation is broken in v3 (see my comment on the issue above). Looks like a small timing difference breaks the order it if the methods are async.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants